<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$garnuDate        = "";
$garnuarray       = array();
$garnuOtherArray  = array();
$garnuText2Tot    = 0;
$garnu            = array();
$garnuId          = array();
$garnuMasterId    = "";
$garnuNameMaster  = "";
$garnuDateMaster  = "";
$weightMaster     = "";
$touchMaster      = "";
$silverMaster     = "";
$copperMaster     = "";
$retWeightMaster  = "";
if(isset($_POST['submitBtn']))
{
  $returnWeight       = $_POST['returnWeight'] == "" ? 0 : $_POST['returnWeight'];
  
  $insertMasterQuery    = "INSERT INTO garnumaster (garnuName,garnuDate,weight,returnWeight,touch,silver,copper)
                           VALUES ('".$_POST['garnuName']."','".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
                           ".$_POST['totalWeight'].",".$returnWeight.",".$_POST['totalWeightTouch'].",".$_POST['totalSilver'].",".$_POST['totalCopper'].")";
  $insertmasterQueryRes = mysql_Query($insertMasterQuery);
  $garnuMasterId = mysql_insert_id();

  $loopCount = 0;
  while($loopCount < count($_POST['weightEntered']))
  {
    $weight = ($_POST['weightEntered'][$loopCount] != "" ) ? $_POST['weightEntered'][$loopCount]: 0;
    $touch =  ($_POST['touchEntered'][$loopCount]  != "" ) ? $_POST['touchEntered'][$loopCount] : 0;
    $silver = ($_POST['silverEntered'][$loopCount] != "" ) ? $_POST['silverEntered'][$loopCount]: 0;
    $copper = ($_POST['copperEntered'][$loopCount] != "" ) ? $_POST['copperEntered'][$loopCount]: 0;
    if($_POST['weightEntered'][$loopCount] != "" || $_POST['touchEntered'][$loopCount] != "" || $_POST['silverEntered'][$loopCount] != "" || $_POST['copperEntered'][$loopCount] != "" )
    {
      $insertDetailQuery = "INSERT INTO garnudetail (garnuMasterId,weight,touch,silver,copper)
                            VALUES (".$garnuMasterId.",".$weight.",".$touch.",".$silver.",".$copper.")";
      $insertDetailQueryRes = mysql_Query($insertDetailQuery);
    }
    $loopCount++;
  }
  
  $loopCountText = 0;
  while($loopCountText < count($_POST['text1']))
  {
    $text1 = ($_POST['text1'][$loopCountText] != "") ? $_POST['text1'][$loopCountText] : 0;
    $text2 = ($_POST['text2'][$loopCountText] != "") ? $_POST['text2'][$loopCountText] : 0;
    if($_POST['text1'][$loopCountText] != "" || $_POST['text2'][$loopCountText] != "" )
    {
      $insertQueryDetail = "INSERT INTO garnuother (garnuMasterId,text1,text2)
                            VALUES (".$garnuMasterId.",'".$text1."',".$text2.")";
	    $insertQueryDetailResult = mysql_query($insertQueryDetail);
    }
    $loopCountText++;
  }
    
}                   
$selectParty = "SELECT garnuMasterId,garnuDate
                  FROM garnumaster";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
while($row = mysql_fetch_assoc($selectPartyRes))
{
  $garnu['garnuDate'][$a]     = $row['garnuDate'];
  $garnu['garnuMasterId'][$a] = $row['garnuMasterId'];
  $a++;
}

$smarty = new Smarty;
require('bottom.php');
$smarty->assign("garnuOtherArray",$garnuOtherArray);
$smarty->assign("garnuText2Tot",$garnuText2Tot);
$smarty->assign("garnuDate",$garnuDate);
$smarty->assign("garnuarray",$garnuarray);
$smarty->assign("garnu",$garnu);
$smarty->assign("garnuId",$garnuId);
$smarty->assign("garnuNameMaster",$garnuNameMaster);
$smarty->assign("garnuDateMaster",$garnuDateMaster);
$smarty->assign("garnuMasterId",$garnuMasterId);
$smarty->assign("weightMaster",$weightMaster);
$smarty->assign("touchMaster",$touchMaster);
$smarty->assign("silverMaster",$silverMaster);
$smarty->assign("copperMaster",$copperMaster);
$smarty->assign("retWeightMaster",$retWeightMaster);
$smarty->display('garnu.tpl');
?>